<?php
class Wee_Logging_Model_Observer{
    protected $_processor;
    public function __construct(){
        $this -> _processor = Mage :: getSingleton('wee_logging/processor');
    }
    public function controllerPredispatch($observer){
        $action = $observer -> getEvent() -> getControllerAction();
        $request = $observer -> getEvent() -> getControllerAction() -> getRequest();
        $beforeForwardInfo = $request -> getBeforeForwardInfo();
        $actionName = $request -> getRequestedActionName();
        if (empty($beforeForwardInfo)){
            $fullActionName = $action -> getFullActionName();
        }else{
            $fullActionName = array($request -> getRequestedRouteName());
            if (isset($beforeForwardInfo['controller_name'])){
                $fullActionName[] = $beforeForwardInfo['controller_name'];
            }else{
                $fullActionName[] = $request -> getRequestedControllerName();
            }
            if (isset($beforeForwardInfo['action_name'])){
                $fullActionName[] = $beforeForwardInfo['action_name'];
            }else{
                $fullActionName[] = $actionName;
            }
            $fullActionName = implode('_', $fullActionName);
        }
        $this -> _processor -> initAction($fullActionName, $actionName);
    }
    public function modelSaveAfter($observer){
        $this -> _processor -> modelActionAfter($observer -> getEvent() -> getObject(), 'save');
    }
    public function modelDeleteAfter($observer){
        $this -> _processor -> modelActionAfter($observer -> getEvent() -> getObject(), 'delete');
    }
    public function modelLoadAfter($observer){
        $this -> _processor -> modelActionAfter($observer -> getEvent() -> getObject(), 'view');
    }
    public function controllerPostdispatch($observer){
        if ($observer -> getEvent() -> getControllerAction() -> getRequest() -> isDispatched()){
            $this -> _processor -> logAction();
        }
    }
    public function adminSessionLoginSuccess($observer){
        $this -> _logAdminLogin($observer -> getUser() -> getUsername(), $observer -> getUser() -> getId());
    }
    public function adminSessionLoginFailed($observer){
        $eventModel = $this -> _logAdminLogin($observer -> getUserName());
    }
    protected function _logAdminLogin($username, $userId = null){
        $eventCode = 'admin_login';
        if (!Mage :: getSingleton('wee_logging/config') -> isActive($eventCode, true)){
            return;
        }
        $success = (bool)$userId;
        if (!$userId){
            $userId = Mage :: getSingleton('admin/user') -> loadByUsername($username) -> getId();
        }
        $request = Mage :: app() -> getRequest();
        return Mage :: getSingleton('wee_logging/event') -> setData(array('ip' => Mage :: helper('core/http') -> getRemoteAddr(), 'user' => $username, 'user_id' => $userId, 'is_success' => $success, 'fullaction' => "{$request->getRouteName()}_{$request->getControllerName()}_{$request->getActionName()}", 'event_code' => $eventCode, 'action' => 'login',)) -> save();
    }
    public function rotateLogs(){
        $lastRotationFlag = Mage :: getModel('wee_logging/flag') -> loadSelf();
        $lastRotationTime = $lastRotationFlag -> getFlagData();
        $rotationFrequency = 3600 * 24 * (int)Mage :: getConfig() -> getNode('default/system/rotation/frequency');
        if (!$lastRotationTime || ($lastRotationTime < time() - $rotationFrequency)){
            Mage :: getResourceModel('wee_logging/event') -> rotate(3600 * 24 * (int)Mage :: getConfig() -> getNode('default/system/rotation/lifetime'));
        }
        $lastRotationFlag -> setFlagData(time()) -> save();
    }
}
